<template>
  <div>
    <el-table border :data="records">
      <el-table-column type="index" label="序号" width="80px" align="center"></el-table-column>
      <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width"></el-table-column>
      <el-table-column label="默认图片" width="110px">
        <template slot-scope="{row,$index}">
          <img :src="row.skuDefaultImg" style="width:80px;height:80px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量（千克）" width="80px"></el-table-column>
      <el-table-column prop="price" label="价格（元）" width="80px"></el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{row,$index}">
          <el-button type="success" icon="el-icon-sort-down" size="mini" v-if="row.isSale!=0" @click="cancel(row)"></el-button>
          <el-button type="info" icon="el-icon-sort-up" size="mini" v-else @click="sale(row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination 
      :total="total" 
      :current-page="page" 
      :page-size="limit" 
      :page-sizes="[3,5,10]"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
      layout="prev,pager,next,jumper,->,sizes,total"
      style="text-align:center">
    </el-pagination>

    <el-drawer :visible.sync="drawer" :show-close="false" size="40%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{skuInfo.skuName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{skuInfo.price}}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag type="success" v-for="(attr,index) in skuInfo.skuAttrValueList" :key="attr.id" style="margin-right:10px">
            {{attr.attrId}}-{{attr.valueId}}
          </el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="300px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
    name: 'Sku',
    data() {
      return {
        page:1,
        limit:3,
        records:[], //存sku列表数据
        total:0,
        skuInfo:{}, //存sku信息
        drawer:false,
      }
    },
    mounted(){
      // 获取sku列表
      this.getSkuList();
    },
    methods:{
      // 获取sku列表数据
      async getSkuList(pager=1){
        this.page = pager;
        const {page,limit} = this;
        let result = await this.$API.sku.reqSkuList(page,limit);
        if(result.code==200){
          this.total = result.data.total;
          this.records = result.data.records;
        }
      },
      // 当分页器某一页条数发生变化回调
      handleSizeChange(limit){
        this.limit = limit;
        this.getSkuList(this.page);
      },
      // 上架
      async sale(row){
        let result = await this.$API.sku.reqSale(row.id);
        if(result.code == 200){
          row.isSale = 1;
          this.$message.success('上架成功');
        }
      },
      // 下架
      async cancel(row){
        let result = await this.$API.sku.reqCancel(row.id);
        if(result.code == 200){
          row.isSale = 0;
          this.$message.success('下架成功');
        }
      },
      // 获取sku详情
      async getSkuInfo(row){
        // 展示抽屉
        this.drawer = true;
        // 发请求获取
        let result = await this.$API.sku.reqSkuById(row.id);
        if(result.code==200){
          this.skuInfo = result.data;
        }
      }
    }
}
</script>

<style>
  .el-row .el-col-5{
    font-size: 18px;
    text-align: right;
  }
  .el-col {
    margin:10px;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
</style>